package com.qzdsoft.erpcloud.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.JWTProperties;
import com.qzdsoft.erpcloud.domain.act.CenterCompany;
import com.qzdsoft.erpcloud.domain.act.CenterCompanyProduct;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.domain.sys.SysUser;
import com.qzdsoft.erpcloud.service.act.CenterCompanyProductService;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.sys.RedisService;
import com.qzdsoft.erpcloud.service.sys.SysMenuButtonService;
import com.qzdsoft.erpcloud.service.sys.SysUserService;
import com.qzdsoft.erpcloud.vo.sys.MenuInfo;
import com.qzdsoft.utils.JsonUtils;
import com.qzdsoft.utils.constant.RedisConstant;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.sys.SysLoginTypeEnum;

/**
 * Created by lifh on 16/10/15.
 */
@Controller
@RequestMapping("/")
public class IndexController extends BaseController{
    
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CenterCompanyService centerCompanyService;

    @Autowired
    private CenterCompanyProductService centerCompanyProductService;

    @Autowired
    private SysMenuButtonService sysMenuButtonService;
    
    @Autowired
    private RedisService redisService;
    
    @Autowired
    private JWTProperties jWTProperties;

    /**
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("")
    public ModelAndView index(HttpServletRequest request, Model model) {
        ModelAndView mav = new ModelAndView();
        LoginUser user = getUser();
        Integer uid = user.getId();
        List<MenuInfo> menus = sysMenuButtonService.selectAuthorizeMenu(uid);
        mav.addObject("menus", menus);
        //存入redis
        //redisService.set(RedisConstant.redisButton+"_"+uid, JsonUtils.objectToJson(authorizedBut));
        redisService.set(RedisConstant.reidsAuthorizedUrl+"_"+uid, JsonUtils.objectToJson(sysMenuButtonService.selectAuthorizedUrl(uid)),jWTProperties.getTokenExpriseTime());
        mav.setViewName("index");
        return mav;
    }


    /**
     * 首页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/home")
    public ModelAndView home(HttpServletRequest request, Model model) {
        ModelAndView mav = new ModelAndView();
        Integer isAgent = -1;
        LoginUser user = getUser();
        Integer lid = user.getId();
        Integer ltype = user.getLoginType();


        SysUser sysUser = null;
        CenterCompany company = null;
        LayTableResponse<CenterCompanyProduct> products = null;
        if(null != ltype){
            if(ltype.equals(SysLoginTypeEnum.user.getKey()) || ltype.equals(SysLoginTypeEnum.company_user.getKey())){
                sysUser = sysUserService.selectById(lid);
                isAgent = null != sysUser && null != sysUser.getIs_agent() ? sysUser.getIs_agent() : -1;
            } else if(ltype.equals(SysLoginTypeEnum.company.getKey())){
                isAgent = 2;
                company = centerCompanyService.getById(lid);
                products = centerCompanyProductService.selectData(null, lid, null,null,null, 0, 100);

            }
        }


        if(null == company){
            company = new CenterCompany();
            company.setSmRate(0);
            company.setVoiceRete(0);
        }

        model.addAttribute("sysUser", null != sysUser ? sysUser : new SysUser());
        model.addAttribute("company", company);
        model.addAttribute("products", null == products ? new ArrayList<CenterCompanyProduct>() : products);

        model.addAttribute("isAgent", isAgent);
        mav.setViewName("home");
        return mav;
    }
}



